#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include "hw2_syscalls.h"


int fibonaci(int n){
	if (n<2)
		return n;
	return fibonaci(n-1)+fibonaci(n-2);

}

int main(int argc, char** argv){
	int i,res;
	struct switch_info array[150];
	if ((argc%2) != 1){
		printf("input error\n");		
		return 0;
	}
	
	for(i=0; i<argc/2; i++){
		struct sched_param param;
		res = fork();
		if (!res){
			param.trial_num=atoi(argv[2*i+1]);
			//OS2: in order for this to work
			//     the following line should be added
			//     in /usr/include/sched.h:
			//     #define trial_num __trial_num
			//     in addition to the change in
			//     /usr/include/bits/sched.h
			param.sched_priority = 0;
			sched_setscheduler(getpid(),SCHED_BRR,&param);	
			fibonaci(atoi(argv[2*(i+1)]));
			return 0;
		}
	}
	
	while (wait(NULL) != -1) {};
	res=get_scheduling_statistic(array);
	printf("%-12s%-12s%-12s%-12s%-12s%-12s\n","prev_pid","next_pid","prev_policy","next_policy","time","reason");
	for (i=0; i<150; i++){
		printf("%-12d%-12d%-12d%-12d%-12lu%-12d\n",
			array[i].previous_pid,
			array[i].next_pid,array[i].previous_policy,
			array[i].next_policy,array[i].time,
			array[i].reason);
	}
	
	return 0;
}
